package mornd.value2;

import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author: mornd
 * @dateTime: 2023/5/11 - 22:58
 *
 *          @Value 注解刨析
 */
@Configuration
public class Demo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Demo.class);
        B1 bean = context.getBean(B1.class);
        System.out.println(bean);

        Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);

        System.out.println(context.getEnvironment().resolvePlaceholders("${Java_Home}"));

    }

    @Component("b1")
    @ToString
    static class B1 {

        @Value("#{@b2}") // @表示按名称注入，可省略
        B2 b2;

        @Value("#{b2.toString}") // 调方法，可省略括号
        String str;


        @Value("#{b2.name}") // 调属性，如果属性和方法同名优先调用方法
        String b2Name;

        @Value("#{T(java.lang.Math).PI}") //  调用静态属性
        private double pi;

        @Value("${Java_Home}")
        private String javaHome;

        @Value("#{'hello, ${Java_Home}'}")
        private String javaHome2;
    }

    @Component("b2")
    static class B2 {
        public String name = "b2 field";

        public String name() {
            return "b2 method";
        }
    }
}
